package com.sxj.PDMS.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxj.PDMS.model.dto.admin.DataInformationRequest;
import com.sxj.PDMS.model.dto.admin.DataInformationUploadRequest;
import com.sxj.PDMS.model.vo.PagingQueryResponse;
import com.sxj.PDMS.model.entity.DataInformationList;
import com.sxj.PDMS.service.DataInformationListService;
import com.sxj.PDMS.mapper.DataInformationListMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import static com.sxj.PDMS.utils.VerifyUtil.formatDate;

/**
 * @author 503c'j
 * @description 针对表【data_information_list(资料信息列表)】的数据库操作Service实现
 * @createDate 2024-06-12 09:49:38
 */
@Service
@Slf4j
public class DataInformationListServiceImpl extends ServiceImpl<DataInformationListMapper, DataInformationList>
        implements DataInformationListService {

    @Autowired
    private DataInformationListMapper dataInformationListMapper;

    @Override
    public Boolean resourceUpload(DataInformationUploadRequest dataInformationUploadRequest) throws ParseException {
        DataInformationList dataInformationList = new DataInformationList();
        BeanUtils.copyProperties(dataInformationUploadRequest, dataInformationList);
        // 获取当前时间
        LocalDateTime currentTime = LocalDateTime.now();
        dataInformationList.setTime(formatDate(currentTime));
        boolean saved = this.save(dataInformationList);
        return saved;

    }

    @Override
    public PagingQueryResponse<DataInformationList> selectAll(Integer page, Integer pageSize) {
        Integer start = (page - 1) * pageSize;
        List<DataInformationList> dataInformationLists = dataInformationListMapper.selectAll(start, pageSize);
        Long total = dataInformationListMapper.selectTotal();
        PagingQueryResponse<DataInformationList> pagingQueryResponse = new PagingQueryResponse<>();

        pagingQueryResponse.setRows(dataInformationLists);

        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }


    public PagingQueryResponse<DataInformationList> dataInformationListSelect(Integer page,
                                                                              Integer pageSize,
                                                                              DataInformationRequest dataInformationRequest) {
        Integer start = (page - 1) * pageSize;
        Integer dataType = dataInformationRequest.getDataType();
        String dataName = dataInformationRequest.getDataName();
        String dataEvent = dataInformationRequest.getAddEvent();
        Date time = dataInformationRequest.getTime();

        List<DataInformationList> dataInformationLists = dataInformationListMapper.selectByTypeOrNameOrEvent(start, pageSize,time, dataType, dataName, dataEvent);
        Long total = dataInformationListMapper.selectTotalByTypeOrNameOrEvent(time,dataType, dataName, dataEvent);

        PagingQueryResponse<DataInformationList> pagingQueryResponse = new PagingQueryResponse<>();
        pagingQueryResponse.setRows(dataInformationLists);
        pagingQueryResponse.setTotal(total);

        return pagingQueryResponse;
    }
}




